# coding=utf-8
# 导入python包
import numpy as np
import argparse
import imutils
import cv2

# 构建并解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
args = vars(ap.parse_args())

# 读取图片
image = cv2.imread(args["image"])

# 寻找到图片中的黑色形状块
lower = np.array([0, 0, 0])
upper = np.array([15, 15, 15])
shapeMask = cv2.inRange(image, lower, upper)

# 在mask中寻找轮廓
cnts = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
print("I found {} black shapes".format(len(cnts)))
cv2.imwrite("Mask.png", shapeMask)
cv2.imshow("Mask", shapeMask)

# 循环遍历所有的轮廓
for c in cnts:
	# draw the contour and show it
	cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
cv2.imwrite("shape1.png", image)
cv2.imshow("Image", image)
cv2.waitKey(0)